package com.sy.olddriver.dao.impl;

import com.sy.olddriver.dao.OrderDao;
import com.sy.olddriver.entity.Order;
import com.sy.olddriver.entity.OrderItem;
import com.sy.olddriver.utils.JdbcHelper;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * OrderDao接口的实现类
 *
 * @author Administrator
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public boolean addOrder(Order order) {
        return JdbcHelper.update(false,"insert into t_order(orderid,uid,createtime) values(?,?,sysdate())", order.getOrderId(), order.getUid());
    }

    @Override
    public boolean addOrderItems(List<OrderItem> orderItems) {
        List<Object> params = new ArrayList<>();
        //values子句中要加入的数据，结果为 (2,'Tom'),(3,'Jack'),(4,'Tony')
        List<String> insertVlaueList = new ArrayList<>();
        StringBuilder sql = new StringBuilder("insert into orderitem(pid,count,oid) values ");
        for (OrderItem orderItem : orderItems) {
            String insertVlaue = "(?,?,?)";
            params.add(orderItem.getPid());
            params.add(orderItem.getCount());
            params.add(orderItem.getOid());
            insertVlaueList.add(insertVlaue);
        }

        sql.append(StringUtils.join(insertVlaueList.iterator(), ","));
        System.out.println(sql);
        return JdbcHelper.update(false,sql.toString(), params.toArray());
    }
}
